package huu.phong.googlemusic;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JDialog {
	private static final long serialVersionUID = 8905523944838053014L;
	Configure configure;
	MusicManager googleMusic;
	JTextField username;
	JPasswordField password;
	JButton ok, cancel;
	JCheckBox autoLogin;
	GoogleMusic main;
	
	public Login(){
		setTitle("Login...");
		setResizable(false);
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		setModal(true);
		setSize(200, 200);
		
		Container container = getContentPane();
		container.setLayout(new FlowLayout());
		JPanel panelLogin = new JPanel(new GridLayout(4, 2, 5, 5));
		panelLogin.add(new JLabel("Email "));
		panelLogin.add(username = new JTextField(15));
		panelLogin.add(new JLabel("Password "));
		panelLogin.add(password = new JPasswordField(15));
		panelLogin.add(new JLabel(""));
		panelLogin.add(autoLogin = new JCheckBox("Auto login", true));
		panelLogin.add (ok = new JButton("OK"));
		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (username.getText().equals("") || password.getPassword().length == 0){
					JOptionPane.showMessageDialog(Login.this, "Email or password is empty !", "Login", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				configure.googleUsername = username.getText();
				configure.googlePassword = Base64Coder.encodeString(new String(password.getPassword()));
				configure.autoLogin = autoLogin.isSelected();
				main.setIconStatus(true);
				main.songInfo.setText("Login to Google Music");
				new Thread(){
					public void run(){
						googleMusic.login(configure.googleUsername, configure.googlePassword);
						if (!googleMusic.logged){
							main.setIconStatus(false);
							main.songInfo.setText("Email or password is wrong !");
							JOptionPane.showMessageDialog(Login.this, "Email or password is wrong !", "Login", JOptionPane.INFORMATION_MESSAGE);
							setVisible(true);
						}else{
							main.songInfo.setText("Get song from Goolge Music");
							try {
								googleMusic.initSongs();
								configure.songs = googleMusic.songs;
								configure.allSong = googleMusic.songs;
								main.setSongs(configure.songs, true);
							} catch (Exception e) {
								e.printStackTrace();
							}
							main.songInfo.setText("Sorting song");
							googleMusic.init();
							main.songInfo.setText("Displaying song");
							main.setSortType();
							setVisible(false);
							main.setIconStatus(false);
							main.songInfo.setText("You are logged to Google Music");
						}
					}
				}.start();
				setVisible(false);
			}
		});
		panelLogin.add(cancel = new JButton("Cancel"));
		cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
		});
		container.add(panelLogin);
		pack();
	}
	
	public void restore(){
		username.setText(configure.googleUsername);
		password.setText(new String(Base64Coder.decode(configure.googlePassword)));
		autoLogin.setSelected(configure.autoLogin);
	}
}
